<?php
namespace Poi\EttsBundle\Model;

/**
 * @author Ricardo J. Vazquez
 * 
 * Representation of a Driver object from the database
 */
class Driver extends Model
{
	public $LicenseNumber = '';
	public $Name = '';
	public $EyeColor = '';
	public $Sex = '';
	public $Direction = '';
	public $Height = 0.0;
	public $Expiration  = '';
	public $Type = '';
	public $Tickets = null; //Tickets
	
	/**
	* Sets up the Tickets property
	*/
	public function init(){
		if(!empty($this->id)){
			$this->LicenseNumber = $this->id;
		}
				
		$this->Tickets = new Tickets($this->conn, $this->LicenseNumber);
		$this->Tickets->setLoadCallback('loadTicketsDriver','Ticket',$this->Tickets);
	}
	
	protected function prepareLoadQuery(){
		if(!empty($this->LicenseNumber)){
			$sql = "SELECT 
						D1.LicenseNumber, 
						D1.Name, 
						D1.Direction, 
						D1.Height, 
						UNIX_TIMESTAMP(D1.Expiration) AS 'Expiration', 
						E1.EyeColor, 
						S1.Sex,
						DT.Type
					FROM Driver D1
						INNER JOIN Eyes E1 on D1.Ojos=E1.idEyes
						INNER JOIN Sex S1 on D1.sexo=S1.idSex
						INNER JOIN DriverType DT on D1.TipoConductor=DT.idDriverType
					WHERE D1.LicenseNumber = :num ";

			$this->loadStmt = $this->conn->prepare($sql);

			$this->loadStmt->bindValue("num", $this->LicenseNumber);
		}	
	}
	
	public function prepareUpdateQuery(){
	}
	
	public function prepareCreateQuery(){
	}
	
	public function prepareDeleteQuery(){
	}
	
	public function toXML(\SimpleXMLElement &$xml=null){
		$return = false;
		if(is_null($xml)){
			$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?><xmlresponse></xmlresponse>');
			$return = true;
		}
		
		$driverXML = $xml->addChild('driver');
		
		$driverXML->addChild('licenseNumber',$this->getLicenseNumber());
		$driverXML->addChild('name',$this->getName());
		$driverXML->addChild('eyeColor',$this->getEyeColor());
		$driverXML->addChild('sex',$this->getSex());
		$driverXML->addChild('address',$this->getDirection());
		$driverXML->addChild('height',$this->getHeight());
		$driverXML->addChild('expiration',$this->getExpiration());
		$driverXML->addChild('type',$this->getType());
		
		$this->getTickets()->toXML($driverXML);
		
		if($return){		
			return $xml->asXML();
		}	
	}
	
	
}